﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Frame.Lib.Core.Tree.Dir;
using Frame.Lib.Utilities;
using Frame.Lib.Common;

public partial class Contact_Outer_DirEdit:PageEdit
{
    private string UpperID
    {
        get
        {
            return Request["UpperID"];
        }
    }
    private DirBO dir = new DirBO("ContactDir");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (PKID != null && PKID != "")
            {
                DispInfo();
            }
            else
            {
                hidUpperID.Value = StrUtil.DecryptDES(UpperID);
                lblUpperName.Text = dir.GetName(hidUpperID.Value);
                txtOrders.Text = "0";
            }
        }
        AddAttr();
    }

    private void AddAttr()
    {

    }

    private void DispInfo()
    {
        DirInfo info = dir.GetInfo(PKID);
        txtName.Text = info.Name.ToString();
        txtOrders.Text = info.Orders.ToString();
        hidUpperID.Value = info.UpperID.ToString();
        if (info.UpperID == 0)
        {
            lblUpperName.Text = "已是顶级目录";
        }
        else
        {
            lblUpperName.Text = dir.GetName(info.UpperID);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strName = txtName.Text.Trim();
        int upperID = int.Parse(hidUpperID.Value);
        bool blnFlag = dir.Exist(upperID, strName, PKID);
        if (blnFlag)
        {
            JSUtil.Alert("该目录已存在");
        }
        else
        {
            DirInfo info = dir.GetInfo(PKID);            
            info.Name = txtName.Text.Trim();
            if (txtOrders.Text.Trim() != "")
            {
                info.Orders = int.Parse(txtOrders.Text);
            }

            if (PKID == null)//新增
            {
                info.CreatorID = AppSession.UserID;
                info.CreatorName = AppSession.UserName;
                info.InputTime = DateTime.Now;

                DirInfo parentInfo = (DirInfo)dir.GetInfo(upperID);
                info.UpperID = upperID;
                info.Depth = parentInfo.Depth + 1;
                info.Path = parentInfo.Path + parentInfo.ID + ",";
            }
            dir.Save(info);

            JSUtil.RefreshView();            
        }
    }  
}
